/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.jeesite.modules.sys.service.support;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.modules.sys.dao.CereBuyerUserDAO;
import com.jeesite.modules.sys.entity.CereBuyerUser;
import com.jeesite.modules.sys.page.BuyerUser;
import com.jeesite.modules.sys.param.LoginParam;
import com.jeesite.modules.sys.service.CereBuyerUserService;
import com.jeesite.modules.sys.service.UserRedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j(topic = "CereBuyerUserServiceImpl")
public class CereBuyerUserServiceImpl implements CereBuyerUserService {

    @Autowired
    private CereBuyerUserDAO cereBuyerUserDAO;

    @Autowired
    private UserRedisService userRedisService;


    @Override
    public CereBuyerUser findByToken(String token) {
        Long buyerUserId = userRedisService.getBuyerUserIdByToken(token);
        if (buyerUserId != null) {
            userRedisService.refreshToken(token);
            CereBuyerUser user = cereBuyerUserDAO.selectByPrimaryKey(buyerUserId);
            user.setToken(token);
            return user;
        }
        return null;
    }

    @Override
    public CereBuyerUser selectByBuyerUserId(Long buyerUserId) {
        CereBuyerUser cereBuyerUser = cereBuyerUserDAO.selectByPrimaryKey(buyerUserId);
        if (cereBuyerUser != null && cereBuyerUser.getWechatName() == null) {
            cereBuyerUser.setWechatName(cereBuyerUser.getName());
        }
        return cereBuyerUser;
    }

    @Override
    public BuyerUser login(LoginParam param) {
        BuyerUser user = cereBuyerUserDAO.findByPhone(param.getPhone());
        // MemberUtil.setMemberInfo(user);
        return user;
    }

    @Override
    public CereBuyerUser getByPhone(String phone) {
        return cereBuyerUserDAO.getByPhone(phone);
    }

    @Override
    public void insert(CereBuyerUser buyerUser) {
        cereBuyerUserDAO.insert(buyerUser);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void update(CereBuyerUser buyerUser) {
        // cereBuyerUserDAO.updatePwd(buyerUser);
        cereBuyerUserDAO.updateByPrimaryKey(buyerUser);
    }

}
